import { createStore, useStore as IvuexStore } from 'vuex'
import type { Store } from 'vuex'
import login from './login/login'
import { IRootState, IStoreType } from './types'
import system from './system/system'
import dashboard from './analysis/dashboard'
import { getPageListData } from '@/service/main/system/system'

const store = createStore<IRootState>({
  state() {
    return {
      name: 'gdh',
      age: 123,
      entireDparment: [],
      entireRole: [],
      entireMenu: []
    }
  },
  mutations: {
    changeDparmentList(state, list) {
      state.entireDparment = list
    },
    changeRoleList(state, list) {
      state.entireRole = list
    },
    changeMenuList(state, list) {
      state.entireMenu = list
    }
  },
  actions: {
    async getInitialDateAction({ commit }) {
      const deparmentResult = await getPageListData('/department/list', {
        offset: 0,
        size: 1000
      })
      // console.log(deparmentResult)
      const { list: deparmentList } = deparmentResult.data
      const roleResult = await getPageListData('/role/list', {
        offset: 0,
        size: 1000
      })
      const { list: roleList } = roleResult.data
      const menuResult = await getPageListData('menu/list', {
        offset: 0,
        size: 1000
      })
      const { list: menuList } = menuResult.data
      commit('changeDparmentList', deparmentList)
      commit('changeRoleList', roleList)
      commit('changeMenuList', menuList)
    }
  },
  modules: {
    login,
    system,
    dashboard
  }
})
export function setupStore() {
  store.dispatch('login/loadLoaclLogin')
  store.dispatch('getInitialDateAction')
}
export function useStore(): Store<IStoreType> {
  return IvuexStore()
}
export default store
